package com.woniu.controller;

import com.woniu.entity.ChargingRecord;
import com.woniu.service.ChargingRecordService;
import com.woniu.util.ResponseData;
import com.woniu.util.ResponseEnum;
import com.woniu.util.easyExcelUtil.EasyExcelUtil;
import com.woniu.vo.ChargingRecordVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * FileName: ChargingRecordController
 * Author: wujiajun
 * Date:   2025/2/17 17:18
 * Description:DOTO
 */
@RestController
@RequestMapping("/chargingRecord")
public class ChargingRecordController {
    @Resource
    private ChargingRecordService chargingRecordService;

    @GetMapping("/list")
    public ResponseData<?> getChargingRecordList(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        List<ChargingRecordVO> records = chargingRecordService.getChargingRecordList(keyword, pageNum, pageSize);
        return ResponseData.ok(records);
    }

    @PostMapping("/add")
    public ResponseData<?> addChargingRecord(@RequestBody ChargingRecord chargingRecord) {
        boolean success = chargingRecordService.addChargingRecord(chargingRecord);
        return success ? ResponseData.ok() : ResponseData.fail(ResponseEnum.FAIL);
    }

    @DeleteMapping("/{id}")
    public ResponseData<?> deleteChargingRecord(@PathVariable("id") Long id) {
        boolean success = chargingRecordService.deleteChargingRecord(id);
        return success ? ResponseData.ok() : ResponseData.fail(ResponseEnum.FAIL);
    }

    @PostMapping("/export")
    public void exportChargingRecords(
            HttpServletResponse response,
            @RequestParam(required = false) String keyword
    ) throws IOException {
        // 设置响应头，防止中文乱码
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        
        // 获取所有充电记录数据
        List<ChargingRecordVO> records = chargingRecordService.getChargingRecordList(keyword, 1, Integer.MAX_VALUE);
        
        // 设置表头
        List<List<String>> headList = new ArrayList<>();
        headList.add(Collections.singletonList("订单号"));
        headList.add(Collections.singletonList("充电电量(度)"));
        headList.add(Collections.singletonList("充电时间"));
        headList.add(Collections.singletonList("充电时长(分钟)"));
        headList.add(Collections.singletonList("金额(元)"));
        headList.add(Collections.singletonList("支付方式"));
        headList.add(Collections.singletonList("交易类型"));
        headList.add(Collections.singletonList("充电类别"));
        headList.add(Collections.singletonList("交易状态"));
        headList.add(Collections.singletonList("充电桩编号"));
        headList.add(Collections.singletonList("充电桩名称"));
        headList.add(Collections.singletonList("客户姓名"));
        headList.add(Collections.singletonList("联系电话"));

        // 转换数据格式
        List<List<Object>> dataList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        for (ChargingRecordVO record : records) {
            List<Object> rowData = new ArrayList<>();
            rowData.add(record.getOrderNumber());
            rowData.add(record.getChargedEnergy());
            rowData.add(record.getChargingTime() != null ? sdf.format(record.getChargingTime()) : "");
            rowData.add(record.getChargingDuration());
            rowData.add(record.getAmount());
            rowData.add(record.getPaymentMethod());
            rowData.add(record.getTransactionType());
            rowData.add(record.getChargingCategory());
            rowData.add(record.getTransactionStatus());
            rowData.add(record.getChargerNumber());
            rowData.add(record.getChargerName());
            rowData.add(record.getCustomerName());
            rowData.add(record.getPhoneNumber());
            dataList.add(rowData);
        }

        // 使用EasyExcel导出
        String fileName = "充电记录_" + System.currentTimeMillis();
        try {
            EasyExcelUtil.writeWithSheetsWeb(response, fileName)
                    .write(headList, dataList, "充电记录")
                    .finish();
        } catch (Exception e) {
            e.printStackTrace();
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println("下载文件失败：" + e.getMessage());
        }
    }

    @PostMapping("/insert")
    public ResponseData<?> insertChargingRecord(@RequestBody ChargingRecord chargingRecord){
        return chargingRecordService.insertChargingRecord(chargingRecord);
    }
}
